package com.sskj.xgja.mine;


import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.sskj.base.data.User;
import com.sskj.xgja.AppManager;
import com.sskj.xgja.LiveDataBus;
import com.sskj.xgja.MVPActivity;
import com.sskj.base.utils.CheckUtil;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.SPUtils;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.MyApplication;
import com.sskj.xgja.R;
import com.sskj.xgja.login.LoginActivity;

import butterknife.BindView;

/**
 * 修改登录密码
 *
 * @author Hey
 * Create at  2019/03/20
 */
public class ResetLoginPasswordActivity extends MVPActivity<ResetLoginPasswordPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolBarLayout;
    @BindView(R.id.mobile)
    TextView mobile;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.edit_password_repeat)
    EditText editPasswordRepeat;
    @BindView(R.id.submit)
    Button submit;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reset_login_password;
    }

    @Override
    public ResetLoginPasswordPresenter getPresenter() {
        return new ResetLoginPasswordPresenter();
    }

    @Override
    public void initView() {
        LiveDataBus.get().with(1, User.class).observe(MyApplication.instance, user1 -> {
                if (mobile!=null&&user1!=null) {
                    mobile.setText(user1.getMobile());
                }
        });

    }

    @Override
    public void initData() {
        toolBarLayout.setLeftButtonOnClickListener(v -> finish());
        ClickUtil.click(getCode, view -> {
            mPresenter.sendCode(mobile.getText().toString());
        });

        ClickUtil.click(submit, view -> {
            if (isEmpty(editCode)) {
                ToastUtils.show(R.string.input_verify_code);
                return;
            }
            if (isEmpty(editPassword)) {
                ToastUtils.show(R.string.input_login_ps);
                return;
            }
            if (isEmpty(editPasswordRepeat)) {
                ToastUtils.show(R.string.input_login_ps_again);
                return;
            }
            if (!editPassword.getText().toString().equals(editPasswordRepeat.getText().toString())) {
                ToastUtils.show(R.string.repeat_ps_error);
                return;
            }
            if (!CheckUtil.isPwd(getText(editPassword))) {
                ToastUtils.show("密码格式错误");
                return;
            }

            mPresenter.resetPassword(getText(mobile), editPassword.getText().toString(), editPasswordRepeat.getText().toString(), editCode.getText().toString());
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ResetLoginPasswordActivity.class);
        context.startActivity(intent);
    }

    public void sendCodeSuccess() {
        startTimeDown(getCode);
    }

    public void resetPasswordSuccess() {
        SPUtils.clear(this, "");
        LoginActivity.start(this);
        AppManager.getInstance().finish2Login();
    }
}
